//
//  VCOfshoucang.m
//  project-A (pianke)
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "VCOfshoucang.h"
#import "CellOfshoucang.h"
#import "model2.h"
#import "webOfdic.h"
#import <DKNightVersion.h>
#import "WebviewControl.h"

#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface VCOfshoucang ()<UICollectionViewDataSource,UICollectionViewDelegate,didshoucangdelegate>

@property (nonatomic,retain)UIView *backview;

@property (nonatomic,retain)UIImageView *image;

@property (nonatomic,retain)UILabel *labelname;

@property (nonatomic,retain)UILabel *labelcount;

@property (nonatomic,retain)UIImageView *imagesex;

@property (nonatomic,retain)UIButton *buttonyouji;

@property (nonatomic,retain)UIButton *buttonxihuan;

@property (nonatomic,retain)UICollectionView *collection;

@property (nonatomic,retain)UIView *blueview;

@property (nonatomic,retain)UIView *viewOffen;


@property (nonatomic,assign)NSInteger count;
@end

@implementation VCOfshoucang

-(void)dealloc{
[_viewOffen release];
    [_blueview release];
    [_collection release];
    [_buttonxihuan release];
    [_buttonyouji release];
    [_image release];
    [_imagesex release];
    [_backview release];
    [_labelcount release];
    [_labelname release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的旅行";
    
    [self creatbackview];
    [self  creatcollection];
    [self creatKVOAction];
    
    
    
}

-(void)creatbackview{
    
    
    
    self.backview = [[UIView alloc]init];
        [self.view addSubview:self.backview];
    self.backview.frame = CGRectMake(0, 64, WIDTH,130);
    self.backview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
    self.viewOffen = [[UIView alloc]initWithFrame:CGRectMake(15,89 , WIDTH - 30, 1)];
    [self.backview addSubview:self.viewOffen];
    self.viewOffen.backgroundColor = [UIColor lightGrayColor];
   

    
    self.image = [[UIImageView alloc]init];
    
    [self.backview addSubview:self.image];
    self.image.frame = CGRectMake(10, 10 , 70, 70);
    self.image.image = [UIImage imageNamed:@"saber"];
    
    self.labelname = [[UILabel alloc]init];
   
    [self.backview addSubview:self.labelname];
    self.labelname.frame = CGRectMake(90,10, 0.5 * WIDTH, 40);
    
    self.labelname.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    self.labelname.text = @"ゞ灬飝翔啲荷蘭亻亼╰→";
    self.labelname.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    
    
    self.labelcount = [[UILabel alloc]init];
    
    [self.backview addSubview:self.labelcount];
    self.labelcount.frame = CGRectMake(90, 60, 0.5 * WIDTH, 20);
    self.labelcount.font = [UIFont fontWithName:@"ArialMT" size:12];
    
     self.labelname.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    self.labelcount.text = @"0篇游记";
    self.labelcount.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    self.imagesex = [[UIImageView alloc]init];
    self.imagesex.frame = CGRectMake(0.5 * WIDTH + 100, 20 , 20, 20);
    [self.backview addSubview:self.imagesex];
    
    self.imagesex.image = [UIImage imageNamed:@"nan"];
    
    
    self.buttonyouji = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonyouji.backgroundColor = [UIColor whiteColor];
    self.buttonyouji.frame = CGRectMake(0, 90 , WIDTH * 0.5, 40);
    
    [self.buttonyouji setTitle:@"游记" forState:UIControlStateNormal];
    
    self.buttonyouji.dk_backgroundColorPicker =  DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
    [self.buttonyouji setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.buttonyouji addTarget:self action:@selector(youji:) forControlEvents:UIControlEventTouchUpInside];
    [self.backview addSubview:self.buttonyouji];
    
    self.buttonxihuan = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonxihuan.frame = CGRectMake(WIDTH * 0.5 , 90, WIDTH  * 0.5,40);
    
    self.buttonxihuan.dk_backgroundColorPicker =  DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    

    
   
    [self.buttonxihuan setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.buttonxihuan setTitle:@"游戏" forState:UIControlStateNormal];
    [self.buttonxihuan addTarget:self action:@selector(youxi:) forControlEvents:UIControlEventTouchUpInside];
    [self.backview addSubview:self.buttonxihuan];
    
    
    self.blueview = [[UIView alloc]initWithFrame:CGRectMake(0, 128 , WIDTH * 0.5, 2)];
    self.blueview.backgroundColor = [UIColor blueColor];
    [self.backview  addSubview:self.blueview];
    
     [self.viewOffen release];
    [self.blueview release];
    [self.labelcount release];
    [self.imagesex release];
    [self.labelname release];
    [self.backview release];
    [self.image release];
    
}


-(void)youji:(UIButton *)buuton{
    
    [self.collection setContentOffset:CGPointMake(0, 0) animated:YES];
    

    
}

-(void)youxi:(UIButton *)buuton{
    
     [self.collection setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    
}

-(void)creatcollection{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 194);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 194, WIDTH, HEIGHT - 194) collectionViewLayout:flowlayout];
    [self.view addSubview:self.collection];
    
    self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);

    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    self.collection.backgroundColor = [UIColor lightGrayColor];
    self.collection.pagingEnabled = YES;
    self.collection.bounces = NO;
    
    [self.collection registerClass:[CellOfshoucang class] forCellWithReuseIdentifier:@"pool1"];
    

    [flowlayout release];
    [self.collection release];


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CellOfshoucang *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        
        cell.delegate = self;
        cell.backgroundColor = [UIColor whiteColor];
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
        
        return cell;
    }else{
        
        
        CellOfshoucang *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        
        cell.delegate = self;
        cell.backgroundColor = [UIColor whiteColor];
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);

        
        return cell;
    }
    
   
}

-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
    
    
}

-(void)creatKVOAction{
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"ddd"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = x * WIDTH / 2 / WIDTH;
    
    self.blueview.transform = CGAffineTransformMakeTranslation(redX, 0);
}

-(void)didClickWith:(webOfdic *)dic{
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"array.plist"];
    
    NSMutableArray *arr =  [NSMutableArray arrayWithContentsOfFile:file];
    
    NSLog(@"%ld",arr.count);
    
    self.count = 0;
    for (webOfdic *dic2 in arr) {
        
        if ([[dic2 objectForKey:@"title"] isEqualToString:[dic objectForKey:@"title"]]) {
            
            self.count += 1;
            NSLog(@"%ld",self.count);
        }else{
            
            self.count += 0;
            
            
            NSLog(@"%ld",self.count);
            
        }
        
        
    }
    
    
    if ([[dic objectForKey:@"video_url"] isEqualToString:@""]) {
       
        WebviewControl *web = [[WebviewControl alloc]init];
        
        NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/mobiles/item/%@?v_=400609&size=middle&t=1453899180",[dic objectForKey:@"idd"]];
        web.urlstring = str;
        
        web.count = self.count;
        
        web.titlea = [dic objectForKey:@"title"];
        web.desc = [dic objectForKey:@"desc"];
        web.video_url = [dic objectForKey:@"video_url"];
        web.published = [dic objectForKey:@"published"];
        
        web.weight_new = [dic objectForKey:@"weight_new"];
        web.idd = [dic objectForKey:@"idd"];
        web.pic_url = [dic objectForKey:@"pic_url"];
        
        
        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
        
    }else{
        
        WebviewControl *web = [[WebviewControl alloc]init];
        
        web.count = self.count;
        web.titlea = [dic objectForKey:@"title"];
        web.desc = [dic objectForKey:@"desc"];
        web.video_url = [dic objectForKey:@"video_url"];
        web.published = [dic objectForKey:@"published"];
        
        web.weight_new = [dic objectForKey:@"weight_new"];
        web.idd = [dic objectForKey:@"idd"];
        web.pic_url = [dic objectForKey:@"pic_url"];

        
        
        NSString *str = [dic objectForKey:@"video_url"];
        web.urlstring = str;
        
        [self.navigationController pushViewController:web animated:YES];
        
        
        [web release];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
